﻿DECLARE @MsgID integer
DECLARE @FromCode integer
DECLARE @ToCode integer
DECLARE @ApName varchar
SET @MsgID = '00'
SET @FromCode = 365
SET @ToCode = 355

SELECT 
  md.datetime_in as SendTime, 
  ab1.surname as FromName,
  ab2.surname as ToName,
  md.msg_size,
  mk.comments,
  md.msg_cirkul,
  md.msg_id,
  md.msg_kind,
  e.item as InfoCode,
  al.item as AliasName,
  h.item as HostName,
  md.from_abonent_id,
  md.to_abonent_id
FROM mon400_data md
INNER JOIN mon400_hostnames h ON md.hostname_id = h.id
INNER JOIN mon400_abonents ab1 ON md.from_abonent_id = ab.id
INNER JOIN mon400_abonents ab2 ON md.to_abonent_id = ab.id
INNER JOIN mon400_aliases al ON md.alias_sender_id = al.id
INNER JOIN mon400_errorcodes e ON md.msg_errorcode_id = e.id
INNER JOIN mon400_msg_categories mk ON md.msg_category = mk.item
WHERE
  (md.msg_id = @MsgID) 
  AND (md.from_abonent_id = @FromCode)
  AND (md.to_abonent_id = @ToCode))
)
ORDER BY
  md.datetime_in, md.msg_cirkul